/*
 * DerbyDao.java
 *
 * Created on 3 de abril de 2006, 23:20
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package fmap.dao;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 *
 * @author javi
 */
public class HsqlDao
{
    
    private static String dbName = "fmap";
    public static Properties dbProperties;
    private static Connection dbConnection;
    private static boolean isConnected = false;

    
    /** Creates a new instance of DerbyDao */
    public HsqlDao ()
    {
        
    }
    
    static
    {
        
        
        String URL = "jdbc:hsqldb:.data/bd/fmap/";
        String username = "sa";
        String password = "";

/*
jdbc:hsqldb:file: mydb
                  /opt/db/accounts
                  C:/data/mydb
*/
        
        try {
            Class.forName("org.hsqldb.jdbcDriver");

            dbConnection = DriverManager.getConnection (
                URL,
                username,
                password);

        
        } catch (Exception e) {
            System.out.println("Failed to load JDBC/ODBC driver.");
        }
/*
        setDBSystemDir ();
        dbProperties = loadDBProperties ();
        loadDatabaseDriver ("org.apache.derby.jdbc.EmbeddedDriver");


        String dbUrl = getDatabaseUrl ();
        try
        {
            dbConnection = DriverManager.getConnection (dbUrl, dbProperties);
            isConnected = dbConnection != null;
        }
        catch (SQLException ex)
        {
            isConnected = false;
        }

        /** Si no existe la BD la creamos */
//        if(!dbExists ())
//        {
            new CreateDb ().createDatabase ();
//        }
    }

    public synchronized static Connection getConnectionHsql ()
    {
        /*
        String dbUrl = getDatabaseUrl ();
        try
        {
            dbConnection = DriverManager.getConnection (dbUrl, dbProperties);
            isConnected = dbConnection != null;
        }
        catch (SQLException ex)
        {
            isConnected = false;
        }
         /**/
        return dbConnection;
    }
    
    
    private static String getDatabaseLocation ()
    {
        String dbLocation = System.getProperty ("derby.system.home") + "/" + dbName;
        return dbLocation;
    }
    
    private static String getDatabaseUrl ()
    {
        String dbUrl = dbProperties.getProperty ("derby.url") + dbName;
        return dbUrl;
    }
    
    private static boolean dbExists ()
    {
        boolean bExists = false;
        String dbLocation = getDatabaseLocation ();
        File dbFileDir = new File (dbLocation);
        if (dbFileDir.exists ())
        {
            bExists = true;
        }
        return bExists;
    }
    
    
    private static void setDBSystemDir ()
    {
        // decide on the db system directory
        String userHomeDir = System.getProperty ("user.home", ".");
        String systemDir = userHomeDir + "/.data/bd/";
        System.setProperty ("derby.system.home", "data/bd/");
//        System.out.println (systemDir);
        
        // create the db system directory
        File fileSystemDir = new File (systemDir);
        fileSystemDir.mkdir ();
    }
    
    private static void loadDatabaseDriver (String driverName)
    {
        // load Derby driver
        try
        {
            Class.forName (driverName);
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace ();
        }
        
    }
    
    private static Properties loadDBProperties ()
    {
        InputStream dbPropInputStream = null;
        dbPropInputStream = fmap.Main.class.getResourceAsStream ("Configuration.properties");
        dbProperties = new Properties ();
        try
        {
            dbProperties.load (dbPropInputStream);
        }
        catch (IOException ex)
        {
            ex.printStackTrace ();
        }
        return dbProperties;
    }
/*
    public static void disconnect ()
    {
        if(isConnected)
        {
            String dbUrl = getDatabaseUrl ();
            dbProperties.put ("shutdown", "true");
            try
            {
                DriverManager.getConnection (dbUrl, dbProperties);
            }
            catch (SQLException ex)
            {
            }
            isConnected = false;
        }
    }
*/
    
}
